home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 38 / Amiga Format CD38 (1999-03-15)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-04].iso / -seriously_amiga- / programming / other / ira_src / ira_2.c < prev    next >
C/C++ Source or Header  |  1999-02-03  |  40KB  |  1,366 lines

  1. ;/*
  2.     failat 20
  3.    sc gst=include:all.gst parms=register nostackcheck opttime optimize IRA_2.c
  4. ;   slink lib:c.o IRA.o IRA20_1.o IRA_2.o to IRA sc sd nd lib lib:sc.lib
  5.  
  6.    QUIT
  7.  
  8.    TABSIZE == 3 !!
  9.  
  10.    Author   : Tim Ruehsen
  11.    Project  : IRA  -  680x0 Interactive ReAssembler
  12.    Part     : IRA_2.c
  13.    Purpose  : Contains data and some subroutines for IRA
  14.    Version  : $VER: IRA_2.c 1.04
  15.    Date     : 23.05.1995
  16.    Copyright: (C)1993-1995 Tim Ruehsen
  17.  
  18. */
  19.  
  20. #include "IRA.h"
  21.  
  22. struct OpCode {
  23.     UBYTE  *mnemonic;
  24.     UWORD result;
  25.     UWORD maske;
  26.     UWORD sourceadr;
  27.     UWORD destadr;
  28.     UWORD cputype;
  29.     UBYTE  flags;
  30.     };
  31.  
  32. char cpuname[][8]={"MC68000","MC68010","MC68020","MC68030","MC68040","MC68060"};
  33.  
  34. char opcode[][8]={
  35. /*  0*/    "BF","","",
  36. /*  3*/    "ILLEGAL","NOP","RESET","RTE","RTR","RTS","RTD","STOP","TRAPV","MOVEC",
  37. /* 13*/    "BKPT","SWAP","LINK","LINK.L","UNLK","EXT","EXT.L","EXTB.L","MOVE.L","MOVE.L","TRAP",
  38. /* 24*/    "DIV","MUL","TAS","JMP","JSR","PEA","NBCD","MOVE","MOVE","MOVE","MOVE",
  39. /* 35*/    "MOVEM","MOVEM","MOVEM.L","MOVEM.L","LEA","CHK","CHK.L","CLR",
  40. /* 43*/    "TST","NOT","NEG","NEGX",
  41. /* 47*/    "ADDA","ADDA.L","ADDX","ADDX","ADD","ADD","EXG","EXG","EXG",
  42. /* 56*/    "ABCD","ABCD","MULS","MULU","AND","AND",
  43. /* 62*/    "CMPA","CMPA.L","CMPM","CMP","EOR",
  44. /* 67*/    "PACK","PACK","UNPK","UNPK","SBCD","SBCD","DIVS","DIVU","OR","OR",
  45. /* 77*/    "MOVE.B","MOVEA","MOVE","MOVEA.L","MOVE.L","MOVEQ",
  46. /* 83*/    "SUBA","SUBA.L","SUBX","SUBX","SUB","SUB",
  47. /* 89*/    "MOVEP","MOVEP","MOVEP.L","MOVEP.L","B","CAS2","CAS","RTM","CALLM","C",
  48. /* 99*/    "CMPI","EORI","ANDI","ADDI","SUBI","ORI","B","MOVES",
  49. /*107*/    "DB","TRAP","TRAP","TRAP","S","ADDQ","SUBQ","B",
  50. /*115*/    "MOVE16","MOVE16","C"," "
  51. };
  52. UWORD result[]={
  53.     0xe8c0,0xe0c0,0xe000,
  54.     0x4afc,0x4e71,0x4e70,0x4e73,0x4e77,0x4e75,0x4e74,0x4e72,0x4e76,0x4e7a,
  55.     0x4848,0x4840,0x4e50,0x4808,0x4e58,0x4880,0x48c0,0x49c0,0x4e68,0x4e60,0x4e40,
  56.     0x4c40,0x4c00,0x4ac0,0x4ec0,0x4e80,0x4840,0x4800,0x44c0,0x46c0,0x40c0,0x42c0,
  57.     0x4880,0x4c80,0x48c0,0x4cc0,0x41c0,0x4180,0x4100,0x4200,
  58.     0x4a00,0x4600,0x4400,0x4000,
  59.     0xd0c0,0xd1c0,0xd100,0xd108,0xd100,0xd000,0xc140,0xc148,0xc188,
  60.     0xc100,0xc108,0xc1c0,0xc0c0,0xc100,0xc000,
  61.     0xb0c0,0xb1c0,0xb108,0xb000,0xb100,
  62.     0x8148,0x8140,0x8188,0x8180,0x8100,0x8108,0x81c0,0x80c0,0x8100,0x8000,
  63.     0x1000,0x3040,0x3000,0x2040,0x2000,0x7000,
  64.     0x90c0,0x91c0,0x9100,0x9108,0x9100,0x9000,
  65.     0x0188,0x0108,0x01c8,0x0148,0x0800,0x08fc,0x08c0,0x06c0,0x06c0,0x00c0,
  66.     0x0c00,0x0a00,0x0200,0x0600,0x0400,0x0000,0x0100,0x0e00,
  67.     0x50c8,0x50fc,0x50fa,0x50fb,0x50c0,0x5000,0x5100,0x6000,
  68.     0xf620,0xf600,0xf400,0x0000
  69.     };
  70.  
  71. UWORD maske[]={
  72.     0xf8c0,0xf8c0,0xf000,
  73.     0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xfffe,
  74.     0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff8,0xfff0,
  75.     0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,0xffc0,
  76.     0xffc0,0xffc0,0xffc0,0xffc0,0xf1c0,0xf1c0,0xf1c0,0xff00,
  77.     0xff00,0xff00,0xff00,0xff00,
  78.     0xf1c0,0xf1c0,0xf138,0xf138,0xf100,0xf100,0xf1f8,0xf1f8,0xf1f8,
  79.     0xf1f8,0xf1f8,0xf1c0,0xf1c0,0xf100,0xf100,
  80.     0xf1c0,0xf1c0,0xf138,0xf100,0xf100,
  81.     0xf1f8,0xf1f8,0xf1f8,0xf1f8,0xf1f8,0xf1f8,0xf1c0,0xf1c0,0xf100,0xf100,
  82.     0xf000,0xf1c0,0xf000,0xf1c0,0xf000,0xf100,
  83.     0xf1c0,0xf1c0,0xf138,0xf138,0xf100,0xf100,
  84.     0xf1f8,0xf1f8,0xf1f8,0xf1f8,0xff00,0xf9ff,0xf9c0,0xfff0,0xffc0,0xf9c0,
  85.     0xff00,0xff00,0xff00,0xff00,0xff00,0xff00,0xf100,0xff00,
  86.     0xf0f8,0xf0ff,0xf0ff,0xf0ff,0xf0c0,0xf100,0xf100,0xf000,
  87.     0xfff8,0xffe0,0xff10,0x0000
  88.     };
  89.  
  90.     /* Achtung ! Gilt nur fuer sourceadr[] und destadr[]. */
  91.     /* Bit 15 (0x8000) : 1 ==> Im unteren Byte steht die einzig moegl. */
  92.     /*                         Adressierungsart. */
  93.     /* Bit 13 (0x2000) : 0 ==> reg=reg2 */
  94.     /*                            1 ==> reg=reg1 */
  95.     /*                            Bestimmt, ob reg1 oder reg2 bestimmend ist. */
  96. UWORD sourceadr[]={
  97.     0x0000,0x0000,0x0000,
  98.     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  99.     0x0000,0x0000,0x8001,0x8001,0x0000,0x0000,0x0000,0x0000,0x800e,0x8001,0x0000,
  100.     0x0bff,0x0bff,0x0000,0x0000,0x0000,0x0000,0x0000,0x0bff,0x0bff,0x800d,0x800c,
  101.     0x800f,0x037e,0x800f,0x037e,0x027e,0x0bff,0x0bff,0x0000,
  102.     0x0bf8,0x0000,0x0000,0x0000,
  103.     0x0fff,0x0fff,0x8000,0x8004,0xa000,0x0fff,0xa000,0xa001,0xa000,
  104.     0x8000,0x8004,0x0bff,0x0bff,0xa000,0x0bff,
  105.     0x0fff,0x0fff,0x8003,0x0fff,0xa000,
  106.     0x8004,0x8000,0x8004,0x8000,0x8000,0x8004,0x0bff,0x0bff,0xa000,0x0bff,
  107.     0x0fff,0x0fff,0x0fff,0x0fff,0x0fff,0x8014,
  108.     0x0fff,0x0fff,0x8000,0x8004,0xa000,0x0fff,
  109.     0xa000,0x8005,0xa000,0x8005,0x8017,0x0000,0x801c,0x0000,0x800b,0x027e,
  110.     0x800b,0x800b,0x800b,0x800b,0x800b,0x800b,0xa017,0x0000,
  111.     0x8000,0x0000,0x800b,0x800b,0x0000,0xa010,0xa010,0x0000,
  112.     0x8003,0x0000,0x8020,0x8000+NOADRMODE
  113.     };
  114. UWORD destadr[]={
  115.     0x8019,0x03f8,0x8000,
  116.     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8016,0x8018,0x0000,0x8021,
  117.     0x8011,0x8000,0x8016,0x801e,0x8001,0x8000,0x8000,0x8000,0x8001,0x800e,0x8013,
  118.     0x801d,0x801d,0x0bf8,0x027e,0x027e,0x027e,0x0bf8,0x800c,0x800d,0x0bf8,0x0bf8,
  119.     0x02f8,0x800f,0x02f8,0x800f,0xa001,0xa000,0xa000,0x0bf8,
  120.     0x0000,0x0bf8,0x0bf8,0x0bf8,
  121.     0xa001,0xa001,0xa000,0xa004,0x03f8,0xa000,0x8000,0x8001,0x8001,
  122.     0xa000,0xa004,0xa000,0xa000,0x03f8,0xa000,
  123.     0xa001,0xa001,0xa003,0xa000,0x0bf8,
  124.     0xa004,0xa000,0xa004,0xa000,0xa000,0xa004,0xa000,0xa000,0x03f8,0xa000,
  125.     0x0bf8,0xa001,0x0bf8,0xa001,0x0bf8,0xa000,
  126.     0xa001,0xa001,0xa000,0xa004,0x03f8,0xa000,
  127.     0x8005,0xa000,0x8005,0xa000,0x0000,0x801b,0x03f8,0x801a,0x027e,0x0000,
  128.     0x0bf8,0x0bf9,0x0bf9,0x0bf8,0x0bf8,0x0bf9,0x0000,0x0000,
  129.     0x8012,0x0000,0x0000,0x0000,0x0bf8,0x0ff8,0x0ff8,0x8015,
  130.     0x801f,0x0000,0x8002,0x0000
  131.     };
  132. /*
  133.     FLAGS:
  134.    0x80 : operand size is in bits [0:1] (0=.B,1=.W,2=.L)
  135.           else use opsize from opcode
  136.    0x40 : operand size has to be appended to mnemonic
  137.     0x20 : one further word has to be saved before further processing.
  138.     0x10 : the condition code identifier has to be appended to the mnemonic.
  139. */
  140. UBYTE flags[]={
  141.     0x20,0x81,0x40,
  142.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x82,
  143.     0x80,0x80,0x81,0x81,0x81,0x81,0x82,0x82,0x82,0x82,0x80,
  144.     0xa2,0xa2,0x80,0x81,0x81,0x80,0x80,0x81,0x81,0x81,0x81,
  145.     0xa1,0xa1,0xa2,0xa2,0x80,0x81,0x82,0x40,
  146.     0x40,0x40,0x40,0x40,
  147.     0x81,0x82,0x40,0x40,0x40,0x40,0x82,0x82,0x82,
  148.     0x80,0x80,0x81,0x81,0x40,0x40,
  149.     0x81,0x82,0x40,0x40,0x40,
  150.     0xa1,0xa1,0xa1,0xa1,0x80,0x80,0x81,0x81,0x40,0x40,
  151.     0x80,0x81,0x81,0x82,0x82,0x80,
  152.     0x81,0x82,0x40,0x40,0x40,0x40,
  153.     0x81,0x81,0x82,0x82,0x00,0x20,0x00,0x00,0x81,0x40,
  154.     0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x40,
  155.     0x90,0x90,0x91,0x92,0x90,0x40,0x40,0x90,
  156.     0x82,0x82,0x00,0x80
  157.     };
  158. UBYTE cputype[]={
  159.     M020UP,M680x0,M680x0,
  160.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M010UP,M680x0,M680x0,M010UP,
  161.     M020UP,M680x0,M680x0,M020UP,M680x0,M680x0,M680x0,M020UP,M680x0,M680x0,M680x0,
  162.     M020UP,M020UP,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M010UP,
  163.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M020UP,M680x0,
  164.     M680x0,M680x0,M680x0,M680x0,
  165.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,
  166.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,
  167.     M680x0,M680x0,M680x0,M680x0,M680x0,
  168.     M020UP,M020UP,M020UP,M020UP,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,
  169.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,
  170.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,
  171.     M680x0,M680x0,M680x0,M680x0,M680x0,M020UP,M020UP,M68020,M68020,M020UP,
  172.     M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M680x0,M010UP,
  173.     M680x0,M020UP,M020UP,M020UP,M680x0,M680x0,M680x0,M680x0,
  174.     M040UP,M040UP,M68040,M680x0
  175.     };
  176.  
  177. char bitshift[][4]   ={"AS","LS","ROX","RO"};
  178. char condcode[][3]   ={
  179.     "T","F","HI","LS","CC","CS","NE","EQ","VC","VS","PL","MI",
  180.     "GE","LT","GT","LE","RA","SR"};
  181. char mmu_cc[][3]     ={
  182.     "BS","BC","LS","LC","SS","SC","AS","AC",
  183.     "WS","WC","IS","IC","GS","GC","CS","CC"};
  184. char fpu_cc[][5]     ={
  185.     "F","EQ","OGT","OGE","OLT","OLE","OGL","OR",
  186.     "UN","UEQ","UGT","UGE","ULT","ULE","NE","T",
  187.     "SF","SEQ","GT","GE","LT","LE","GL","GLE",
  188.     "NGLE","NGL","NLE","NLT","NGE","NGT","SNE","ST"};
  189. char extension[][3]  ={".B","",".L"};
  190. char caches[][3]     ={"NC","DC","IC","BC"};
  191. char bitop[][4]      ={"TST","CHG","CLR","SET"};
  192. char memtypename[][7]= {"PUBLIC","CHIP","FAST","???"};
  193. char modname[][5]    ={"CODE","DATA","BSS"};
  194. char bitfield[][5]   ={"TST","EXTU","CHG","EXTS","CLR","FFO","SET","INS"};
  195. char cregname[][6]   ={"SFC","DFC","CACR","TC", "ITT0","ITT1", "DTT0","DTT1","BUSCR",
  196.                        "USP","VBR","CAAR","MSP","ISP", "MMUSR","URP", "SRP","PCR"};
  197. UWORD cregflag[18]   ={M010UP,M010UP,M020UP,M040UP,M040UP,M040UP,M040UP,M040UP,M68060,
  198.                        M010UP,M010UP,M68020|M68030,M020UP,M020UP,M68040,M040UP,M040UP,M68060};
  199.  
  200. struct x_adr x_adrs[] = {
  201.     "ABSEXECBASE",0x0004,
  202.     "BUS_ERROR",  0x0008,
  203.     "ADR_ERROR",  0x000C,
  204.     "ILLEG_OPC",  0x0010,
  205.     "DIVISION0",  0x0014,
  206.     "CHK",        0x0018,
  207.     "TRAPV",      0x001C,
  208.     "PRIVILEG",   0x0020,
  209.     "TRACE",      0x0024,
  210.     "LINEA_EMU",  0x0028,
  211.     "LINEF_EMU",  0x002C,
  212.     "INT_NOINI",  0x003C,
  213.     "INT_WRONG",  0x0060,
  214.     "AUTO_INT1",  0x0064,
  215.     "AUTO_INT2",  0x0068,
  216.     "AUTO_INT3",  0x006C,
  217.     "AUTO_INT4",  0x0070,
  218.     "AUTO_INT5",  0x0074,
  219.     "AUTO_INT6",  0x0078,
  220.     "NMI",        0x007C,
  221.     "TRAP_01",    0x0080,
  222.     "TRAP_02",    0x0084,
  223.     "TRAP_03",    0x0088,
  224.     "TRAP_04",    0x008C,
  225.     "TRAP_05",    0x0090,
  226.     "TRAP_06",    0x0094,
  227.     "TRAP_07",    0x0098,
  228.     "TRAP_08",    0x009C,
  229.     "TRAP_09",    0x00A0,
  230.     "TRAP_10",    0x00A4,
  231.     "TRAP_11",    0x00A8,
  232.     "TRAP_12",    0x00AC,
  233.     "TRAP_13",    0x00B0,
  234.     "TRAP_14",    0x00B4,
  235.     "TRAP_15",    0x00B8,
  236.     "CIAB_PRA",   0xBFD000,
  237.     "CIAB_PRB",   0xBFD100,
  238.     "CIAB_DDRA",  0xBFD200,
  239.     "CIAB_DDRB",  0xBFD300,
  240.     "CIAB_TALO",  0xBFD400,
  241.     "CIAB_TAHI",  0xBFD500,
  242.     "CIAB_TBLO",  0xBFD600,
  243.     "CIAB_TBHI",  0xBFD700,
  244.     "CIAB_TDLO",  0xBFD800,
  245.     "CIAB_TDMD",  0xBFD900,
  246.     "CIAB_TDHI",  0xBFDA00,
  247.     "CIAB_SDR",   0xBFDC00,
  248.     "CIAB_ICR",   0xBFDD00,
  249.     "CIAB_CRA",   0xBFDE00,
  250.     "CIAB_CRB",   0xBFDF00,
  251.     "CIAA_PRA",   0xBFE001,
  252.     "CIAA_PRB",   0xBFE101,
  253.     "CIAA_DDRA",  0xBFE201,
  254.     "CIAA_DDRB",  0xBFE301,
  255.     "CIAA_TALO",  0xBFE401,
  256.     "CIAA_TAHI",  0xBFE501,
  257.     "CIAA_TBLO",  0xBFE601,
  258.     "CIAA_TBHI",  0xBFE701,
  259.     "CIAA_TDLO",  0xBFE801,
  260.     "CIAA_TDMD",  0xBFE901,
  261.     "CIAA_TDHI",  0xBFEA01,
  262.     "CIAA_SDR",   0xBFEC01,
  263.     "CIAA_ICR",   0xBFED01,
  264.     "CIAA_CRA",   0xBFEE01,
  265.     "CIAA_CRB",   0xBFEF01,
  266.     "CLK_S1",     0xDC0000,
  267.     "CLK_S10",    0xDC0004,
  268.     "CLK_MI1",    0xDC0008,
  269.     "CLK_MI10",   0xDC000C,
  270.     "CLK_H1",     0xDC0010,
  271.     "CLK_H10",    0xDC0014,
  272.     "CLK_D1",     0xDC0018,
  273.     "CLK_D10",    0xDC001C,
  274.     "CLK_MO1",    0xDC0020,
  275.     "CLK_MO10",   0xDC0024,
  276.     "CLK_Y1",     0xDC0028,
  277.     "CLK_Y10",    0xDC002E,
  278.     "CLK_WEEK",   0xDC0030,
  279.     "CLK_CD",     0xDC0034,
  280.     "CLK_CE",     0xDC0038,
  281.     "CLK_CF",     0xDC003C,
  282.     "HARDBASE",   0xDFF000,
  283.     "DMACONR",    0xDFF002,
  284.     "VPOSR",      0xDFF004,
  285.     "VHPOSR",     0xDFF006,
  286.     "DSKDATR",    0xDFF008,
  287.     "JOY0DAT",    0xDFF00A,
  288.     "JOY1DAT",    0xDFF00C,
  289.     "CLXDAT",     0xDFF00E,
  290.     "ADKCONR",    0xDFF010,
  291.     "POT0DAT",    0xDFF012,
  292.     "POT1DAT",    0xDFF014,
  293.     "POTGOR",     0xDFF016,
  294.     "SERDATR",    0xDFF018,
  295.     "DSKBYTR",    0xDFF01A,
  296.     "INTENAR",    0xDFF01C,
  297.     "INTREQR",    0xDFF01E,
  298.     "DSKPTH",     0xDFF020,
  299.     "DSKPTL",     0xDFF022,
  300.     "DSKLEN",     0xDFF024,
  301.     "DSKDAT",     0xDFF026,
  302.     "REFPTR",     0xDFF028,
  303.     "VPOSW",      0xDFF02A,
  304.     "VHPOSW",     0xDFF02C,
  305.     "COPCON",     0xDFF02E,
  306.     "SERDAT",     0xDFF030,
  307.     "SERPER",     0xDFF032,
  308.     "POTGO",      0xDFF034,
  309.     "JOYTEST",    0xDFF036,
  310.     "STREQU",     0xDFF038,
  311.     "STRVBL",     0xDFF03A,
  312.     "STRHOR",     0xDFF03C,
  313.     "STRLONG",    0xDFF03E,
  314.     "BLTCON0",    0xDFF040,
  315.     "BLTCON1",    0xDFF042,
  316.     "BLTAFWM",    0xDFF044,
  317.     "BLTALWM",    0xDFF046,
  318.     "BLTCPTH",    0xDFF048,
  319.     "BLTCPTL",    0xDFF04A,
  320.     "BLTBPTH",    0xDFF04C,
  321.     "BLTBPTL",    0xDFF04E,
  322.     "BLTAPTH",    0xDFF050,
  323.     "BLTAPTL",    0xDFF052,
  324.     "BLTDPTH",    0xDFF054,
  325.     "BLTDPTL",    0xDFF056,
  326.     "BLTSIZE",    0xDFF058,
  327.     "BLTCON01",   0xDFF05A, /* ECS */
  328.     "BLTSIZV",    0xDFF05C, /* ECS */
  329.     "BLTSIZH",    0xDFF05E, /* ECS */
  330.     "BLTCMOD",    0xDFF060,
  331.     "BLTBMOD",    0xDFF062,
  332.     "BLTAMOD",    0xDFF064,
  333.     "BLTDMOD",    0xDFF066, /* 50 */
  334.     "BLTCDAT",    0xDFF070,
  335.     "BLTBDAT",    0xDFF072,
  336.     "BLTADAT",    0xDFF074,
  337.     "BLTDDAT",    0xDFF076,
  338.     "SPRHDAT",    0xDFF078, /* ECS */
  339.     "DENISEID",   0xDFF07C, /* ECS */
  340.     "DSKSYNC",    0xDFF07E,
  341.     "COP1LCH",    0xDFF080,
  342.     "COP1LCL",    0xDFF082,
  343.     "COP2LCH",    0xDFF084,
  344.     "COP2LCL",    0xDFF086,
  345.     "COPJMP1",    0xDFF088,
  346.     "COPJMP2",    0xDFF08A,
  347.     "COPINS",     0xDFF08C,
  348.     "DIWSTRT",    0xDFF08E,
  349.     "DIWSTOP",    0xDFF090,
  350.     "DDFSTRT",    0xDFF092,
  351.     "DFFSTOP",    0xDFF094,
  352.     "DMACON",     0xDFF096,
  353.     "CLXCON",     0xDFF098,
  354.     "INTENA",     0xDFF09A,
  355.     "INTREQ",     0xDFF09C,
  356.     "ADKCON",     0xDFF09E,
  357.     "AUD0LCH",    0xDFF0A0,
  358.     "AUD0LCL",    0xDFF0A2,
  359.     "AUD0LEN",    0xDFF0A4,
  360.     "AUD0PER",    0xDFF0A6,
  361.     "AUD0VOL",    0xDFF0A8,
  362.     "AUD0DAT",    0xDFF0AA,
  363.     "AUD1LCH",    0xDFF0B0,
  364.     "AUD1LCL",    0xDFF0B2,
  365.     "AUD1LEN",    0xDFF0B4,
  366.     "AUD1PER",    0xDFF0B6,
  367.     "AUD1VOL",    0xDFF0B8,
  368.     "AUD1DAT",    0xDFF0BA,
  369.     "AUD2LCH",    0xDFF0C0,
  370.     "AUD2LCL",    0xDFF0C2,
  371.     "AUD2LEN",    0xDFF0C4,
  372.     "AUD2PER",    0xDFF0C6,
  373.     "AUD2VOL",    0xDFF0C8,
  374.     "AUD2DAT",    0xDFF0CA,
  375.     "AUD3LCH",    0xDFF0D0,
  376.     "AUD3LCL",    0xDFF0D2,
  377.     "AUD3LEN",    0xDFF0D4,
  378.     "AUD3PER",    0xDFF0D6,
  379.     "AUD3VOL",    0xDFF0D8,
  380.     "AUD3DAT",    0xDFF0DA,
  381.     "BPL1PTH",    0xDFF0E0,
  382.     "BPL1PTL",    0xDFF0E2,
  383.     "BPL2PTH",    0xDFF0E4,
  384.     "BPL2PTL",    0xDFF0E6,
  385.     "BPL3PTH",    0xDFF0E8,
  386.     "BPL3PTL",    0xDFF0EA,
  387.     "BPL4PTH",    0xDFF0EC,
  388.     "BPL4PTL",    0xDFF0EE,
  389.     "BPL5PTH",    0xDFF0F0,
  390.     "BPL5PTL",    0xDFF0F2,
  391.     "BPL6PTH",    0xDFF0F4,
  392.     "BPL6PTL",    0xDFF0F6,
  393.     "BPLCON0",    0xDFF100,
  394.     "BPLCON1",    0xDFF102,
  395.     "BPLCON2",    0xDFF104,
  396.     "BPLCON3",    0xDFF106, /* ECS */
  397.     "BPL1MOD",    0xDFF108,
  398.     "BPL2MOD",    0xDFF10A,
  399.     "BPL1DAT",    0xDFF110,
  400.     "BPL2DAT",    0xDFF112,
  401.     "BPL3DAT",    0xDFF114,
  402.     "BPL4DAT",    0xDFF116,
  403.     "BPL5DAT",    0xDFF118,
  404.     "BPL6DAT",    0xDFF11A,
  405.     "SPR0PTH",    0xDFF120,
  406.     "SPR0PTL",    0xDFF122,
  407.     "SPR1PTH",    0xDFF124,
  408.     "SPR1PTL",    0xDFF126,
  409.     "SPR2PTH",    0xDFF128,
  410.     "SPR2PTL",    0xDFF12A,
  411.     "SPR3PTH",    0xDFF12C,
  412.     "SPR3PTL",    0xDFF12E,
  413.     "SPR4PTH",    0xDFF130,
  414.     "SPR4PTL",    0xDFF132,
  415.     "SPR5PTH",    0xDFF134,
  416.     "SPR5PTL",    0xDFF136,
  417.     "SPR6PTH",    0xDFF138,
  418.     "SPR6PTL",    0xDFF13A,
  419.     "SPR7PTH",    0xDFF13C,
  420.     "SPR7PTL",    0xDFF13E,
  421.     "SPR0POS",    0xDFF140,
  422.     "SPR0CTL",    0xDFF142,
  423.     "SPR0DATA",   0xDFF144,
  424.     "SPR0DATB",   0xDFF146,
  425.     "SPR1POS",    0xDFF148,
  426.     "SPR1CTL",    0xDFF14A,
  427.     "SPR1DATA",   0xDFF14C,
  428.     "SPR1DATB",   0xDFF14E,
  429.     "SPR2POS",    0xDFF150,
  430.     "SPR2CTL",    0xDFF152,
  431.     "SPR2DATA",   0xDFF154,
  432.     "SPR2DATB",   0xDFF156,
  433.     "SPR3POS",    0xDFF158,
  434.     "SPR3CTL",    0xDFF15A,
  435.     "SPR3DATA",   0xDFF15C,
  436.     "SPR3DATB",   0xDFF15E,
  437.     "SPR4POS",    0xDFF160,
  438.     "SPR4CTL",    0xDFF162,
  439.     "SPR4DATA",   0xDFF164,
  440.     "SPR4DATB",   0xDFF166,
  441.     "SPR5POS",    0xDFF168,
  442.     "SPR5CTL",    0xDFF16A,
  443.     "SPR5DATA",   0xDFF16C,
  444.     "SPR5DATB",   0xDFF16E,
  445.     "SPR6POS",    0xDFF170,
  446.     "SPR6CTL",    0xDFF172,
  447.     "SPR6DATA",   0xDFF174,
  448.     "SPR6DATB",   0xDFF176,
  449.     "SPR7POS",    0xDFF178,
  450.     "SPR7CTL",    0xDFF17A,
  451.     "SPR7DATA",   0xDFF17C,
  452.     "SPR7DATB",   0xDFF17E,
  453.     "COLOR00",    0xDFF180,
  454.     "COLOR01",    0xDFF182,
  455.     "COLOR02",    0xDFF184,
  456.     "COLOR03",    0xDFF186,
  457.     "COLOR04",    0xDFF188,
  458.     "COLOR05",    0xDFF18A,
  459.     "COLOR06",    0xDFF18C,
  460.     "COLOR07",    0xDFF18E,
  461.     "COLOR08",    0xDFF190,
  462.     "COLOR09",    0xDFF192,
  463.     "COLOR10",    0xDFF194,
  464.     "COLOR11",    0xDFF196,
  465.     "COLOR12",    0xDFF198,
  466.     "COLOR13",    0xDFF19A,
  467.     "COLOR14",    0xDFF19C,
  468.     "COLOR15",    0xDFF19E,
  469.     "COLOR16",    0xDFF1A0,
  470.     "COLOR17",    0xDFF1A2,
  471.     "COLOR18",    0xDFF1A4,
  472.     "COLOR19",    0xDFF1A6,
  473.     "COLOR20",    0xDFF1A8,
  474.     "COLOR21",    0xDFF1AA,
  475.     "COLOR22",    0xDFF1AC,
  476.     "COLOR23",    0xDFF1AE,
  477.     "COLOR24",    0xDFF1B0,
  478.     "COLOR25",    0xDFF1B2,
  479.     "COLOR26",    0xDFF1B4,
  480.     "COLOR27",    0xDFF1B6,
  481.     "COLOR28",    0xDFF1B8,
  482.     "COLOR29",    0xDFF1BA,
  483.     "COLOR30",    0xDFF1BC,
  484.     "COLOR31",    0xDFF1BE,
  485.     "HTOTAL",     0xDFF1C0, /* Ab hier nur ECS-Register */
  486.     "HSSTOP",     0xDFF1C2,
  487.     "HBSTRT",     0xDFF1C4,
  488.     "HBSTOP",     0xDFF1C6,
  489.     "VTOTAL",     0xDFF1C8,
  490.     "VSSTOP",     0xDFF1CA,
  491.     "VBSTRT",     0xDFF1CC,
  492.     "VBSTOP",     0xDFF1CE,
  493.     "SPRHSTRT",   0xDFF1D0,
  494.     "SPRHSTOP",   0xDFF1D2,
  495.     "BPLHSTRT",   0xDFF1D4,
  496.     "BPLHSTOP",   0xDFF1D6,
  497.     "HHPOSW",     0xDFF1D8,
  498.     "HHPOSR",     0xDFF1DA,
  499.     "BEAMCON0",   0xDFF1DC,
  500.     "HSSTRT",     0xDFF1DE,
  501.     "VSSTRT",     0xDFF1E0,
  502.     "HCENTER",    0xDFF1E2,
  503.     "DIWHIGH",    0xDFF1E4,
  504.     "BPLHMOD",    0xDFF1E6,
  505.     "SPRHPTH",    0xDFF1E8,
  506.     "SPRHPTL",    0xDFF1EA,
  507.     "BPLHPTH",    0xDFF1EC,
  508.     "BPLHPTL",    0xDFF1EE,
  509.    "FMODE",      0xDFF1FE
  510.     };
  511.  
  512.  
  513. extern UBYTE
  514.   **SymbolName,
  515.     StdName[STDNAMELENGTH];
  516.  
  517. extern void
  518.     InsertSymbol(UBYTE *, ULONG value),
  519.     InsertCodeAdr(ULONG);
  520.  
  521. extern UWORD
  522.    *buffer,
  523.    *memtype,
  524.    *DRelocBuffer;
  525.  
  526. extern ULONG
  527.     PASS,
  528.     prgstart,
  529.     prgende,
  530.     prgcount,
  531.     pflags,
  532.     basesec,
  533.     SymbolCount,
  534.    *SymbolValue,
  535.     LabelMax,
  536.    *LabelAdr,
  537.    *LabelAdr2,
  538.     labcount,
  539.     relocount,
  540.     relocmax,
  541.    *RelocAdr,
  542.    *RelocVal,
  543.    *RelocOff,
  544.    *RelocMod,
  545.    *RelocBuffer,
  546.     RelocNumber,
  547.     nextreloc,
  548.   **modulstrt,
  549.    *moduloffs,
  550.    *modultab,
  551.    *modultype,
  552.     modulcount,
  553.     modulcnt,
  554.     LastModul,
  555.     XRefCount,
  556.     LabX_len,
  557.    *XRefListe;
  558.  
  559. extern FILE
  560.     *sourcefile,
  561.     *targetfile,
  562.     *binfile;
  563.  
  564. extern void
  565.       Freemem(void *ptr,ULONG cnt),
  566.       ExitPrg(char *, ...);
  567.  
  568. void
  569.       chkabort(void);
  570.  
  571.  
  572.  
  573. void *GetPMem(ULONG len)
  574. {
  575. void *ptr=0;
  576.  
  577.     if (len) {
  578.         if (!(ptr = AllocMem(len,MEMF_CLEAR)))
  579.             ExitPrg("Not enough memory (%lu Bytes) !\n",len);
  580.         /* printf("Allo: %08X  %8ld\n",ptr,len); */
  581.     }
  582.     return(ptr);
  583. }
  584. ULONG FileLength(UBYTE *name)
  585. {
  586. LONG len;
  587. FILE *file;
  588.     if (name) {
  589.         if (!(file = fopen(name,"r")))
  590.             ExitPrg("Can't open %s\n",name);
  591.         if (fseek(file,0,SEEK_END))
  592.             ExitPrg("seek error (%s)\n",name);
  593.         if ((len = ftell(file)) == -1L)
  594.             ExitPrg("ftell error (%s)\n",name);
  595.         fclose(file);
  596.     }
  597.     else printf("FileLength: Got no Name!\n");
  598.     return ((ULONG)len);
  599. }
  600. void *GetNewVarBuffer(void *p,ULONG size)
  601. {
  602. void *np;
  603.     np = GetPMem(size*8);
  604.     lmovmem(p,np,size);
  605.     Freemem(p,size*4);
  606.     return(np);
  607. }
  608. int _abort(void)
  609. {
  610.     fflush(stdout);
  611.     ExitPrg("***User-Break\n");
  612.     return 1;
  613. }
  614. ULONG ReadSymbol(FILE *file,ULONG *val,UBYTE *type)
  615. {
  616. ULONG length,dummy;
  617.     if ((fread(&length,4,1,file)) != 1) ExitPrg("ReadSymbol error (1)\n");
  618.     if (!length) return(0);
  619.     if (type) {
  620.         *type   = (length>>24);
  621.         length &= 0x00FFFFFF;
  622.     }
  623.     length*=4;
  624.     if (length >= STDNAMELENGTH) dummy=STDNAMELENGTH-1;
  625.     else dummy=length;
  626.     if ((fread(StdName,1,dummy,file)) != dummy) ExitPrg("ReadSymbol error (2)\n");
  627.     StdName[dummy] = 0;
  628.     if (length > dummy) fseek(file,length-dummy,SEEK_CUR);
  629.     if (val)
  630.         if ((fread(val,4,1,file)) != 1) ExitPrg("ReadSymbol error (3)\n");
  631.     return(dummy);
  632. }
  633. void InsertReloc(ULONG adr,ULONG value,LONG offs,ULONG mod)
  634. /*
  635.    adr     Adresse, auf der reloziert wird
  636.    value   Inhalt der Adresse (stellt auch eine Adresse dar)
  637. */
  638. {
  639. ULONG l=0,m,r=relocount;
  640.  
  641.     /* Dieser Fall tritt sehr haeufig auf */
  642.     if (adr > RelocAdr[relocount-1] && relocount) {
  643.             RelocAdr[relocount]   = adr;
  644.             RelocVal[relocount]   = value;
  645.             RelocOff[relocount]   = offs;
  646.             RelocMod[relocount++] = mod;
  647.     }
  648.     else {
  649.         /* Binaeres Suchen von adr */
  650.         while (l<r) {
  651.             m=(l+r)/2;
  652.             if (RelocAdr[m]<adr) l=m+1;
  653.             else                 r=m;
  654.         }
  655.         if (RelocAdr[r]!=adr || r==relocount) {
  656.             lmovmem(&RelocAdr[r],&RelocAdr[r+1],relocount-r);
  657.             lmovmem(&RelocOff[r],&RelocOff[r+1],relocount-r);
  658.             lmovmem(&RelocVal[r],&RelocVal[r+1],relocount-r);
  659.             lmovmem(&RelocMod[r],&RelocMod[r+1],relocount-r);
  660.             RelocAdr[r] = adr;
  661.             RelocOff[r] = offs;
  662.             RelocVal[r] = value;
  663.             RelocMod[r] = mod;
  664.             relocount++;
  665.         }
  666.     }
  667.     if (relocount == relocmax) {
  668.         RelocAdr = GetNewVarBuffer(RelocAdr,relocmax);
  669.         RelocVal = GetNewVarBuffer(RelocVal,relocmax);
  670.         RelocOff = GetNewVarBuffer(RelocOff,relocmax);
  671.         RelocMod = GetNewVarBuffer(RelocMod,relocmax);
  672.         relocmax *= 2;
  673.     }
  674.  
  675. }
  676. void InsertLabel(LONG adr)
  677. {
  678. ULONG l=0,m,r=labcount;
  679.  
  680.     if (PASS == 0) return;
  681.  
  682.     /* Dieser Fall tritt sehr haeufig auf */
  683.     if (adr > (long)LabelAdr[labcount-1] && labcount) {
  684.         LabelAdr[labcount++] = adr;
  685.     }
  686.     else {
  687.         /* Binaeres Suchen von adr */
  688.         while (l<r) {
  689.             m=(l+r)/2;
  690.             if ((long)LabelAdr[m]<adr) l=m+1;
  691.             else                         r=m;
  692.         }
  693.         if (LabelAdr[r]!=adr || r==labcount) {
  694.             lmovmem(&LabelAdr[r],&LabelAdr[r+1],labcount-r);
  695.             LabelAdr[r] = adr;
  696.             labcount++;
  697.         }
  698.     }
  699.     if (labcount == LabelMax) {
  700.         LabelAdr  = GetNewVarBuffer(LabelAdr,LabelMax);
  701.         LabelMax *= 2;
  702.     }
  703. }
  704. void InsertXref(ULONG adr)
  705. {
  706. ULONG  l=0,m,r=XRefCount;
  707.  
  708.     if (PASS == 0) return;
  709.  
  710.     /* Binaeres Suchen von adr */
  711.     while (l<r) {
  712.         m=(l+r)/2;
  713.         if (XRefListe[m]<adr) l=m+1;
  714.         else                  r=m;
  715.     }
  716.     if (XRefListe[r]!=adr || r==XRefCount) {
  717.         lmovmem(&XRefListe[r],&XRefListe[r+1],XRefCount-r);
  718.         XRefListe[r]   = adr;
  719.         XRefCount++;
  720.     }
  721.  
  722.     if (XRefCount == LabX_len) {
  723.         XRefListe   = GetNewVarBuffer(XRefListe,LabX_len);
  724.         LabX_len *= 2;
  725.     }
  726. }
  727.  
  728. extern UWORD opcstart[16];
  729. extern UWORD opccount[16];
  730. extern UWORD SIZEOF_RESULT;
  731. extern ULONG ADRCOUNT;
  732. void InitOpcode(void)
  733. {
  734. UWORD i;
  735.  
  736.     SIZEOF_RESULT=sizeof(result);
  737.     ADRCOUNT=sizeof(x_adrs)/sizeof(struct x_adr);
  738.     for(i=0;i<(sizeof(result))/sizeof(UWORD)-1;i++) {
  739.         if (opccount[result[i]>>12]==0) opcstart[result[i]>>12]=i;
  740.         opccount[result[i]>>12]++;
  741.     }
  742.  
  743.     if (sizeof(result)!=sizeof(sourceadr))
  744.         ExitPrg("sizeof(result) != sizeof(sourceadr)");
  745.     if (sizeof(result)!=sizeof(destadr))
  746.         ExitPrg("sizeof(result) != sizeof(destadr)");
  747.     if (sizeof(result)!=sizeof(maske))
  748.         ExitPrg("sizeof(result) != sizeof(maske)");
  749.     if (sizeof(result)/sizeof(UWORD)!=sizeof(flags)/sizeof(UBYTE))
  750.         ExitPrg("sizeof(result) != sizeof(flags)");
  751.  
  752. }
  753. int GetSymbol(ULONG adr)
  754. {
  755. ULONG i;
  756.  
  757.     // printf("GETSYMBOL(%08X)\n",adr);
  758.  
  759.     for(i=0;i<SymbolCount;i++) {
  760.         if (SymbolValue[i] == adr) {
  761.             adrcat(SymbolName[i]);
  762.             return(-1);
  763.         }
  764.     }
  765.     return(0);
  766. }
  767.  
  768. void GetLabel(LONG adr,UWORD adrmode)
  769. {
  770. ULONG dummy=-1;
  771. char  buf[20];
  772. ULONG l=0,m,r=labcount,r2;
  773.  
  774.     /* Search for an entry in LabelAdr */
  775.     while (l<r) {
  776.         m=(l+r)/2;
  777.         if ((long)LabelAdr[m]<adr) l=m+1;
  778.         else                       r=m;
  779.     }
  780.     if (LabelAdr[r]!=adr) {
  781.         fprintf(stderr,"ADR=%08x not found! (mode=%d) relocount=%ld nextreloc=%ld\n",adr,adrmode,relocount,nextreloc);
  782.         fprintf(stderr,"LabelAdr[l=%lu]=%08x\n",l,LabelAdr[l]);
  783.         fprintf(stderr,"LabelAdr[m=%lu]=%08x\n",m,LabelAdr[m]);
  784.         fprintf(stderr,"LabelAdr[r=%lu]=%08x\n\n",r,LabelAdr[r]);
  785.         adrcat("LAB_");
  786.         adrcat(itohex(adr,8));
  787.         return;
  788.     }
  789.  
  790.     /* to avoid several label at the same address */
  791.     r2=r;
  792.     while(r && (LabelAdr2[r]==LabelAdr2[r-1])) r--;
  793.  
  794.     /* Pass 2 */
  795.     if (adrmode==9999) {
  796.         if (LabelAdr2[r] == moduloffs[RelocMod[nextreloc]]) {
  797.             if (!GetSymbol(LabelAdr[r2])) {
  798.                 adrcat("SECSTRT_");
  799.                 adrcat(itoa(RelocMod[nextreloc]));
  800.             }
  801.             if (dummy=RelocOff[nextreloc]) {
  802.                 if ((LONG)RelocOff[nextreloc] > 0) adrcat("+");
  803.                 adrcat(itoa(RelocOff[nextreloc]));
  804.             }
  805.             else if (dummy = LabelAdr[r2]-LabelAdr2[r]) {
  806.                 adrcat("+");
  807.                 adrcat(itoa(dummy));
  808.             }
  809.         } else {
  810.             if (!GetSymbol(LabelAdr[r2])) {
  811.                 sprintf(buf,"LAB_%04lX",r);
  812.                 adrcat(buf);
  813.             }
  814.             if (dummy = LabelAdr[r2]-LabelAdr2[r]) {
  815.                 adrcat("+");
  816.                 adrcat(itoa(dummy));
  817.             }
  818.         }
  819.     } else if (LabelAdr2[r] == moduloffs[modulcnt] && adrmode!=5) {
  820.         if (!GetSymbol(LabelAdr[r2])) {
  821.             adrcat("SECSTRT_");
  822.             adrcat(itoa(modulcnt));
  823.         }
  824.         if (adr > (long)moduloffs[modulcnt]) {
  825.             adrcat("+");
  826.             adrcat(itoa(adr-moduloffs[modulcnt]));
  827.         }
  828.         else if (adr < (long)moduloffs[modulcnt])
  829.             adrcat(itoa(adr-moduloffs[modulcnt]));
  830.     } else {
  831.         if (!GetSymbol(LabelAdr[r2])) {
  832.             if (adrmode==5 && LabelAdr2[r2]==moduloffs[basesec]) {
  833.                 adrcat("SECSTRT_");
  834.                 adrcat(itoa(basesec));
  835.             }
  836.             else {
  837.                 sprintf(buf,"LAB_%04lX",r);
  838.                 adrcat(buf);
  839.             }
  840.         }
  841.         if (dummy = LabelAdr[r2]-LabelAdr2[r]) {
  842.             adrcat("+");
  843.             adrcat(itoa(dummy));
  844.         }
  845.     }
  846. }
  847. void GetExtName(ULONG index)
  848. {
  849. register ULONG xref;
  850. ULONG  l=0,m,r=ADRCOUNT;
  851.  
  852.     xref = XRefListe[index];
  853.     if (xref >= 0xDC0000 && xref<=0xDCFFFF) xref&=0xDC00FC;
  854.  
  855.     /* Binaere Suche nach Eintrag */
  856.     while (l<r) {
  857.         m=(l+r)/2;
  858.         if (x_adrs[m].adr<xref) l=m+1;
  859.         else                    r=m;
  860.     }
  861.     if (x_adrs[r].adr!=xref) {
  862.         adrcat("EXT_");
  863.         adrcat(itohex(index,4));
  864.     }
  865.     else
  866.         adrcat(&x_adrs[r].name[0]);
  867. }
  868. void GetXref(ULONG adr)
  869. {
  870. ULONG l=0,m,r=XRefCount;
  871.  
  872.     /* Vorhandenen Eintrag in XRefListe suchen */
  873.     while (l<r) {
  874.         m=(l+r)/2;
  875.         if (XRefListe[m]<adr) l=m+1;
  876.         else                  r=m;
  877.     }
  878.     if (XRefListe[r]!=adr) {
  879.         fprintf(stderr,"XRef ADR=%08x not found!\n",adr);
  880.         adrcat("EXT_");
  881.         adrcat(itohex(adr,8));
  882.     }
  883.     else GetExtName(r);
  884. }
  885. void ExamineHunks(void)
  886. {
  887. char   modulname[STDNAMELENGTH];
  888. UBYTE  type;
  889. ULONG  i,dummy,offset,offs,value;
  890. ULONG  relocnt,relocnt1;
  891. UWORD  nextmodul=0,out_of_range=0,DREL32BUF[2];
  892. ULONG  modullen=0,hunk,relomod;
  893. ULONG  OVL_Size,OVL_Level,OVL_Data[8];
  894.  
  895.  
  896.     modulname[0]=0;
  897.  
  898.     for(offs=prgstart,i=0;i<modulcount;i++) {
  899.         memtype[i]   = (modultab[i]>>30);     /* PUBLIC,CHIP,FAST,EXTENSION */
  900.         /* calculate offsets for relocation */
  901.         modultab[i] *= 4;
  902.         moduloffs[i] = offs;
  903.         offs += modultab[i];
  904.  
  905.         /* get memory for the modules */
  906.         modulstrt[i] = GetPMem(modultab[i]);
  907.     }
  908.  
  909.     /* read modules and relocate */
  910.     for(i=0;i<modulcount;) {
  911.  
  912.         /* sort of modul (Code,Data,...) */
  913.         if ((fread(&hunk,4,1,sourcefile)) != 1) break;
  914.  
  915.         hunk   &= 0x0000ffff;
  916.  
  917.         switch (hunk) {
  918.             case 0x03E9: /* CODE */
  919.             case 0x03EA: /* DATA */
  920.             case 0x03EB: /* BSS  */
  921.                     i+=nextmodul;
  922.                     nextmodul=1;
  923.  
  924.                     if (memtype[i] == 3) fread(&dummy,4,1,sourcefile); /* Aufwaertskompatibel */
  925.  
  926.                     modultype[i] = hunk;
  927.                     fread(&modullen,4,1,sourcefile); /* length of module */
  928.  
  929.                     /* Evtl. Overlay-Hunks */
  930.                     if (i > LastModul) {
  931.                         printf("i > LastModul\n");
  932.                         /* Offsets fuer Relokation errechnen */
  933.                         modultab[i]  = modullen*4;
  934.                         moduloffs[i] = offs;
  935.                         offs += modultab[i];
  936.                         /* Speicher fuer Module beschaffen */
  937.                         modulstrt[i] = GetPMem(modultab[i]);
  938.                     }
  939.  
  940.                     if (hunk != 0x03EB)      /* Nur bei Code und Data */
  941.                         fread(modulstrt[i],4,modullen,sourcefile); /* Langwoerter in Speicher */
  942.  
  943.                     if (pflags&SHOW_RELOCINFO) {
  944.                         printf("\n    Module %d : %s ,%-6s",i,modname[modultype[i]-0x03E9],memtypename[memtype[i]]);
  945.                         if (modulname[0]) {
  946.                             printf(" ,Name='%s'",modulname);
  947.                             modulname[0]=0;
  948.                         }
  949.                         if (modultype[i] == 0x03EB)
  950.                             printf(" ,%ld Bytes.\n",modultab[i]);
  951.                         else {
  952.                             printf(" ,%ld Bytes",modullen*4);
  953.                             if (modultab[i]-modullen*4)
  954.                                 printf(" (+ %ld BSS).\n",modultab[i]-modullen*4);
  955.                             else
  956.                                 printf(".\n");
  957.                         }
  958.                     }
  959.                 break;
  960.             case 0x03F8: /* HUNK_DREL16 */
  961.             case 0x03F9: /* HUNK_DREL8  */
  962.             case 0x03ED: /* HUNK_RELOC16 */
  963.             case 0x03EE: /* HUNK_RELOC8  */
  964.                     relocnt1 = 0;
  965.                     do {
  966.                         /* read number of reloctions */
  967.                         if ((fread(&relocnt,4,1,sourcefile)) != 1) break;
  968.                         relocnt1 += relocnt;
  969.                         if (relocnt) fseek(sourcefile,(relocnt+1)*4,SEEK_CUR);
  970.                     } while (relocnt);
  971.                     if (pflags&SHOW_RELOCINFO)
  972.                         printf("      Hunk_(D)Reloc16/8: %ld entries\n",relocnt1);
  973.                 break;
  974.             case 0x03F7: /* HUNK_DREL32 (V37+) */
  975.             case 0x03FC: /* HUNK_RELOC32SHORT (V39+) */
  976.                     if (pflags&SHOW_RELOCINFO) {
  977.                         if (hunk==0x03F7) printf("      Hunk_DRel32: ");
  978.                         if (hunk==0x03FC) printf("      Hunk_Reloc32Short: ");
  979.                     }
  980.                     relocnt1 = 0;
  981.                     do {
  982.                         /* read number of relocations */
  983.                         if ((fread(DREL32BUF,sizeof(DREL32BUF),1,sourcefile)) != 1) break;
  984.                         if (!(relocnt = DREL32BUF[0])) break;
  985.                         relocnt1 += relocnt;
  986.  
  987.                         /* Bezugsmodul einlesen */
  988.                         relomod = DREL32BUF[1];
  989.                         if (relomod > LastModul)
  990.                             ExitPrg("Relocation: Bad Modul (%ld)\n",relomod);
  991.  
  992.                         /* Relokation durchfuehren */
  993.                         RelocNumber  = relocnt;
  994.                         DRelocBuffer = GetPMem(RelocNumber*sizeof(UWORD));
  995.                         fread(DRelocBuffer,sizeof(UWORD),RelocNumber,sourcefile);
  996.                         while (relocnt--) {
  997.                             offset = DRelocBuffer[relocnt];
  998.                             if ((long)offset<0 || offset>(modultab[i]-4))
  999.                                 ExitPrg("Relocation: Bad offset (0 <= (offset=%ld) <= %ld)\n",offset,modultab[i]-4);
  1000.                             dummy = *((long *)((ULONG)modulstrt[i]+offset));
  1001.                             if ((long)dummy<0L || dummy>=modultab[relomod]) out_of_range=1;
  1002.                             dummy += (long)moduloffs[relomod];
  1003.                             *((ULONG *)((ULONG)modulstrt[i]+offset)) = dummy;
  1004.  
  1005.                             if (out_of_range) { /* HUNK-Uebergreifende Labels */
  1006. /* printf("out_of_range In Hunk %ld  To Hunk %ld  $%08X - $%08X  $%08X\n",i,relomod,moduloffs[relomod],moduloffs[relomod]+modultab[relomod],dummy); */
  1007.                                 InsertReloc(moduloffs[i]+offset,moduloffs[relomod],dummy-moduloffs[relomod],relomod);
  1008.                                 InsertLabel(moduloffs[relomod]);
  1009.                                 out_of_range=0;
  1010.                             }
  1011.                             else {
  1012.                                 InsertReloc(moduloffs[i]+offset,dummy,0L,relomod);
  1013.                                 InsertLabel(dummy);
  1014.                             }
  1015.  
  1016.                         }
  1017.                         Freemem (DRelocBuffer,RelocNumber*2);
  1018.                         DRelocBuffer = 0;
  1019.                     } while (1);
  1020.                     if (pflags&SHOW_RELOCINFO)
  1021.                         printf("%ld entries\n",relocnt1);
  1022.                 break;
  1023.             case 0x03EC: /* HUNK_RELOC32 */
  1024.                     if (pflags&SHOW_RELOCINFO) {
  1025.                         if (hunk==0x03EC) printf("      Hunk_Reloc32: ");
  1026.                     }
  1027.                     relocnt1 = 0;
  1028.                     do {
  1029.                         /* read number of relocations */
  1030.                         if ((fread(&relocnt,4,1,sourcefile)) != 1) break;
  1031.                         if (!relocnt) break;
  1032.                         relocnt1 += relocnt;
  1033.  
  1034.                         /* Bezugsmodul einlesen */
  1035.                         if ((fread(&relomod,4,1,sourcefile)) != 1) break;
  1036.                         if (relomod > LastModul)
  1037.                             ExitPrg("Relocation: Bad Modul (%d)\n",relomod);
  1038.  
  1039.                         /* Relokation durchfuehren */
  1040.                         RelocNumber = relocnt;
  1041.                         RelocBuffer = GetPMem(RelocNumber*4);
  1042.                         fread(RelocBuffer,4,RelocNumber,sourcefile);
  1043.                         while (relocnt--) {
  1044.                             offset = RelocBuffer[relocnt];
  1045.                             if ((long)offset<0 || offset>(modultab[i]-4))
  1046.                                 ExitPrg("Relocation: Bad offset (0 <= (offset=%ld) <= %ld)\n",offset,modultab[i]-4);
  1047.                             dummy = *((long *)((ULONG)modulstrt[i]+offset));
  1048.                             if ((long)dummy<0L || dummy>=modultab[relomod]) out_of_range=1;
  1049.                             dummy += (long)moduloffs[relomod];
  1050.                             *((ULONG *)((ULONG)modulstrt[i]+offset)) = dummy;
  1051.  
  1052.                             if (out_of_range) { /* HUNK-Uebergreifende Labels */
  1053. /* printf("out_of_range In Hunk %ld  To Hunk %ld  $%08X - $%08X  $%08X\n",i,relomod,moduloffs[relomod],moduloffs[relomod]+modultab[relomod],dummy); */
  1054.                                 InsertReloc(moduloffs[i]+offset,moduloffs[relomod],dummy-moduloffs[relomod],relomod);
  1055.                                 InsertLabel(moduloffs[relomod]);
  1056.                                 out_of_range=0;
  1057.                             }
  1058.                             else {
  1059.                                 InsertReloc(moduloffs[i]+offset,dummy,0L,relomod);
  1060.                                 InsertLabel(dummy);
  1061.                             }
  1062.  
  1063.                         }
  1064.                         Freemem (RelocBuffer,RelocNumber*4);
  1065.                         RelocBuffer = 0;
  1066.                     } while (1);
  1067.                     if (pflags&SHOW_RELOCINFO)
  1068.                         printf("%ld entries\n",relocnt1);
  1069.                 break;
  1070.             case 0x03F5: /* HUNK_OVERLAY */
  1071.                     fread(&OVL_Size,4,1,sourcefile);
  1072.                     fread(&OVL_Level,4,1,sourcefile);
  1073.                     fread(&dummy,4,1,sourcefile);
  1074.                     fseek (sourcefile, -4, SEEK_CUR);
  1075.                     if (dummy == 0) {
  1076.                         OVL_Level -= 2;
  1077.                         OVL_Size = (OVL_Size-OVL_Level+1)/8;
  1078.                         fseek(sourcefile,(OVL_Level+1)*4,SEEK_CUR);
  1079.                     }
  1080.                     else
  1081.                         OVL_Size = OVL_Size/8;
  1082.                     if (pflags&SHOW_RELOCINFO)
  1083.                         printf("\n    Hunk_Overlay: %ld Level, %ld Entries\n",OVL_Level,OVL_Size);
  1084.                     while (OVL_Size--) {
  1085.                         fread(&OVL_Data,4,8,sourcefile);
  1086.                         if (pflags&SHOW_RELOCINFO) {
  1087.                             printf("      SeekOffset: $%08lx\n",OVL_Data[0]);
  1088.                             printf("      Dummy1    : %ld\n",OVL_Data[1]);
  1089.                             printf("      Dummy2    : %ld\n",OVL_Data[2]);
  1090.                             printf("      Level     : %ld\n",OVL_Data[3]);
  1091.                             printf("      Ordinate  : %ld\n",OVL_Data[4]);
  1092.                             printf("      FirstHunk : %ld\n",OVL_Data[5]);
  1093.                             printf("      SymbolHunk: %ld\n",OVL_Data[6]);
  1094.                             printf("      SymbolOffX: %08lx\n\n",OVL_Data[7]);
  1095.                         }
  1096.                     }
  1097.                 break;
  1098.             case 0x03F2: /* HUNK_END   */
  1099.             case 0x03F6: /* HUNK_BREAK */
  1100.                     i+=nextmodul;
  1101.                     nextmodul=0;
  1102.                 break;
  1103.             case 0x03E8: /* HUNK_NAME */
  1104.                     ReadSymbol(sourcefile,0,0);
  1105.                     strcpy(modulname,StdName);
  1106.                 break;
  1107.             case 0x03F1: /* HUNK_DEBUG */
  1108.                     if (pflags&SHOW_RELOCINFO) printf("      hunk_debug (skipped).\n");
  1109.                     fread(&dummy,4,1,sourcefile);
  1110.                     fseek(sourcefile,dummy*4,SEEK_CUR);
  1111.                 break;
  1112.             case 0x03F0: /* HUNK_SYMBOL */
  1113.                     if (pflags&SHOW_RELOCINFO) printf("      hunk_symbol:\n");
  1114.                     while (dummy=ReadSymbol(sourcefile,&value,0)) {
  1115.                         value += (moduloffs[i]);
  1116.                         if (pflags&SHOW_RELOCINFO) printf("        %s = %08x\n",StdName,value);
  1117.                         InsertSymbol(StdName, value);
  1118.                         InsertLabel(value);
  1119.                     }
  1120.                 break;
  1121.             case 0x03EF: /* HUNK_EXT */
  1122.                     if (pflags&SHOW_RELOCINFO) printf("      hunk_ext:\n");
  1123.                     do {
  1124.                         dummy=ReadSymbol(sourcefile,&value,&type);
  1125.                         if (dummy) {
  1126.                             switch (type) {
  1127.                                 ULONG ref;
  1128.                                 case 0: /* EXT_SYMB */
  1129.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_symb:\n");
  1130.                                     if (pflags&SHOW_RELOCINFO) printf("          %s = %08lx\n",StdName,value);
  1131.                                     break;
  1132.                                 case 1: /* EXT_DEF  */
  1133.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_def:\n");
  1134.                                     if (pflags&SHOW_RELOCINFO) printf("          %s = %08lx\n",StdName,value);
  1135.                                     break;
  1136.                                 case 2: /* EXT_ABS  */
  1137.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_abs:\n");
  1138.                                     if (pflags&SHOW_RELOCINFO) printf("          %s = %08lx\n",StdName,value);
  1139.                                     break;
  1140.                                 case 3: /* EXT_RES  */
  1141.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_res:\n");
  1142.                                     if (pflags&SHOW_RELOCINFO) printf("          %s = %08lx\n",StdName,value);
  1143.                                     break;
  1144.                                 case 130: /* EXT_COMMON */
  1145.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_common:\n");
  1146.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, Size=%ld\n",StdName,value);
  1147.                                     fread(&value,4,1,sourcefile);
  1148.                                     fseek(sourcefile,value*4,SEEK_CUR);
  1149.                                     break;
  1150.                                 case 129: /* EXT_REF32 */
  1151.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_ref32:\n");
  1152.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1153.                                     while (value--) {
  1154.                                         fread(&ref,4,1,sourcefile);
  1155.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1156.                                     }
  1157.                                     break;
  1158.                                 case 131: /* EXT_REF16 */
  1159.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_ref16:\n");
  1160.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1161.                                     while (value--) {
  1162.                                         fread(&ref,4,1,sourcefile);
  1163.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1164.                                     }
  1165.                                     break;
  1166.                                 case 132: /* EXT_REF8 */
  1167.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_ref8:\n");
  1168.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1169.                                     while (value--) {
  1170.                                         fread(&ref,4,1,sourcefile);
  1171.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1172.                                     }
  1173.                                     break;
  1174.                                 case 133: /* EXT_DEXT32 */
  1175.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_dext32:\n");
  1176.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1177.                                     while (value--) {
  1178.                                         fread(&ref,4,1,sourcefile);
  1179.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1180.                                     }
  1181.                                     break;
  1182.                                 case 134: /* EXT_DEXT16 */
  1183.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_dext16:\n");
  1184.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1185.                                     while (value--) {
  1186.                                         fread(&ref,4,1,sourcefile);
  1187.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1188.                                     }
  1189.                                     break;
  1190.                                 case 135: /* EXT_DEXT8 */
  1191.                                     if (pflags&SHOW_RELOCINFO) printf("        ext_dext8:\n");
  1192.                                     if (pflags&SHOW_RELOCINFO) printf("          %s, %ld reference(s)\n",StdName,value);
  1193.                                     while (value--) {
  1194.                                         fread(&ref,4,1,sourcefile);
  1195.                                         if (pflags&SHOW_RELOCINFO) printf("          %08lx\n",ref);
  1196.                                     }
  1197.                                     break;
  1198.                                 default:
  1199.                                     ExitPrg("Unknown HUNK_EXT sub-type=%d !\n",type);
  1200.                                     break;
  1201.                             }
  1202.                         }
  1203.                     } while (dummy);
  1204.                 break;
  1205.             case 0x03E7: /* HUNK_UNIT */
  1206.             case 0x03FA: /* HUNK_LIB     */
  1207.             case 0x03FB: /* HUNK_INDEX   */
  1208.             default:
  1209.                     ExitPrg("Hunk...:%08x  NOT SUPPORTED.\n",hunk);
  1210.                 break;
  1211.  
  1212.         } /* End of switch() */
  1213.  
  1214.     } /* read next modul */
  1215.     printf("\n");
  1216.  
  1217.     /* write data to file and release memory */
  1218.     for(i=0;i<modulcount;i++) {
  1219.         fwrite(modulstrt[i],1,modultab[i],binfile);
  1220.         Freemem(modulstrt[i],modultab[i]);
  1221.     }
  1222.     Freemem(modulstrt,modulcount*sizeof(ULONG *));
  1223.     modulstrt = 0;
  1224. }
  1225.  
  1226. void CreateSymbol(char *name,ULONG symptr,ULONG refptr,ULONG module,ULONG number)
  1227. {
  1228. char symbol[32];
  1229.  
  1230.     strcpy(symbol,name);
  1231.     if (number) strcat(symbol,itoa(number));
  1232.     InsertReloc(refptr+prgstart,symptr,0L,module);
  1233.     InsertSymbol(symbol,symptr);
  1234.     InsertLabel(symptr);
  1235. }
  1236.  
  1237. void SearchRomTag(void)
  1238. {
  1239. UBYTE name[80];
  1240. ULONG number=0;
  1241. ULONG ptr,refptr,functable,module=0;
  1242. LONG  i,j,k,l;
  1243. UBYTE flags,Type;
  1244. UWORD relative=0;
  1245. char *FuncName[]={"OPEN","CLOSE","EXPUNGE","RESERVED","BEGINIO","ABORTIO"};
  1246.  
  1247.     for(i=0;i<(LONG)(prgende-prgstart-24)/2;i++) {
  1248.         if (buffer[i] == 0x4AFC) {
  1249.             i+=1;
  1250.             ptr = *((ULONG *)&buffer[i]);
  1251.  
  1252.             /* OK. RomTag structure found */
  1253.             if ((ptr-prgstart) == (i-1)*2) {
  1254.                 for(l=0;l<=LastModul;l++) {
  1255.                     if (modultab[l]) {
  1256.                         if (i*2 >= moduloffs[l] && i*2 < moduloffs[l]+modultab[l]) {
  1257.                             module=l;
  1258.                             break;
  1259.                         }
  1260.                     }
  1261.                 }
  1262.                 if (i==1) pflags |= ROMTAGatZERO;
  1263.  
  1264.                 CreateSymbol("ROMTAG",ptr,i*2,module,number);
  1265.                 i+=2;
  1266.                 CreateSymbol("ENDSKIP",*((ULONG *)&buffer[i]),i*2,module,number);
  1267.                 i+=2;
  1268.                 flags=(UBYTE)(buffer[i++]>>8);
  1269.                 Type =(UBYTE)(buffer[i++]>>8);
  1270.                 ptr = *((ULONG *)&buffer[i]);
  1271.                 strncpy(name,(char *)ptr,16);
  1272.                 name[15]=0;
  1273.                 strupr(name);
  1274.                 for(k=0;k<16;k++) if (!isalnum(name[k])) name[k]=0;
  1275.                 if (Type==NT_LIBRARY) {
  1276.                     strcat(name,"LIBNAME");
  1277.                     CreateSymbol(name,ptr,i*2,module,0);
  1278.                 }
  1279.                 else if (Type==NT_DEVICE) {
  1280.                     strcat(name,"DEVNAME");
  1281.                     CreateSymbol(name,ptr,i*2,module,0);
  1282.                 }
  1283.                 else if (Type==NT_RESOURCE) {
  1284.                     strcat(name,"RESNAME");
  1285.                     CreateSymbol(name,ptr,i*2,module,0);
  1286.                 }
  1287.                 else {
  1288.                     strcat(name,"NAME");
  1289.                     CreateSymbol(name,ptr,i*2,module,number);
  1290.                 }
  1291.                 i+=2;
  1292.  
  1293.                 CreateSymbol("IDSTRING",*((ULONG *)&buffer[i]),i*2,module,number);
  1294.                 i+=2;
  1295.                 ptr = *((ULONG *)&buffer[i]);
  1296.                 CreateSymbol("INIT",ptr,i*2,module,number);
  1297.                 i+=2;
  1298.                 /* if RTF_AUTOINIT is set, INIT points to a special structure. */
  1299.                 if (flags&0x80) {
  1300.                     j=(ptr-prgstart)/2;
  1301.                     ptr=((ULONG *)&buffer[j])[3];
  1302.                     if (ptr) {
  1303.                         CreateSymbol("INITFUNCTION",ptr,(j+6)*2,module,number);
  1304.                         InsertCodeAdr(ptr);
  1305.                     }
  1306.                     ptr=((ULONG *)&buffer[j])[2];
  1307.                     if (ptr) {
  1308.                         CreateSymbol("DATATABLE",ptr,(j+4)*2,module,number);
  1309.                     }
  1310.                     functable=((ULONG *)&buffer[j])[1];
  1311.                     if (functable) {
  1312.                         CreateSymbol("FUNCTABLE",functable,(j+2)*2,module,number);
  1313.                         functable-=prgstart;
  1314.                         j=functable/2;
  1315.                         if (buffer[j] == 0xFFFF) relative=1;
  1316.  
  1317.                         k=j+relative;
  1318.                         l=0;
  1319.                         if (Type == NT_DEVICE) l=6;
  1320.                         if (Type == NT_LIBRARY) l=4;
  1321.                         ptr=0;
  1322.                         while (ptr != 0xFFFFFFFF) {
  1323.                             if (relative==1) {
  1324.                                 if (buffer[k]==0xFFFF) ptr=0xFFFFFFFF;
  1325.                                 else ptr=functable+(WORD)buffer[k];
  1326.                             }
  1327.                             else {
  1328.                                 ptr=((ULONG *)&buffer[j])[k-j];
  1329.                                 refptr=j*2+(k-j)*4;
  1330.                             }
  1331.                             k++;
  1332.                             if (ptr && (ptr != 0xFFFFFFFF)) {
  1333.                                 if (k-j > l) {
  1334.                                     strcpy(name,"LIBFUNC");
  1335.                                     if (number) strcat(name,itoa(number));
  1336.                                     strcat(name,"_");
  1337.                                     strcat(name,itoa(k-j-l-1));
  1338.                                 }
  1339.                                 else {
  1340.                                     strcpy(name,FuncName[k-j-1-relative]);
  1341.                                     if (number) strcat(name,itoa(number));
  1342.                                 }
  1343.                                 if (relative==0)
  1344.                                     InsertReloc(refptr+prgstart,ptr+prgstart,0L,module);
  1345.                                 InsertSymbol(name,ptr+prgstart);
  1346.                                 InsertCodeAdr(ptr+prgstart);
  1347.                                 InsertLabel(ptr+prgstart);
  1348.                             }
  1349.                         }
  1350.                     }
  1351.                 }
  1352.                 else {
  1353.                     InsertCodeAdr(ptr);
  1354.                 }
  1355.                 number ++;
  1356.             }
  1357.         }
  1358.     }
  1359. }
  1360. void WriteTarget(void *ptr,ULONG len)
  1361. {
  1362.     chkabort();
  1363.     if ((fwrite(ptr,1,len,targetfile)) != len)
  1364.         ExitPrg("Write Error !\n");
  1365. }
  1366.